﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Arc.DairyMan.DairyMan.BusinessLogic;
using Arc.DairyMan.DairyMan.Common;
using Arc.DairyMan.DairyMan.Model;
using Arc.WinForms.Controls;
using System.Collections;

namespace Arc.DairyMan.DairyMan.UI
{
    public partial class frmSabhasad : ArcForm
    {
        #region Variables & Properties
        DataSet _DS = new DataSet();
        DataSet _DSSearch = new DataSet();
        Sabhasad Model;
        blSabhasad BL;
        blSabhasad _blSabhasad;
        #endregion

        #region Constructors
        public frmSabhasad()
        {
            InitializeComponent();
            BL = new blSabhasad();
            Model = (Sabhasad)BL.ModelObj;
            BL.DS = _DS;
            BL.Search("1<>1");
            //grdSabhasad.Cols.Fixed = 0;

            _blSabhasad = new blSabhasad();
            _blSabhasad.DS = _DSSearch;

            ArrayList _MilkTypes = new ArrayList();

            cmbMilkType.DisplayMember = "Name";
            cmbMilkType.ValueMember = "Value";
            foreach (var str in Enum.GetNames(typeof(MilkType)))
            {
                _MilkTypes.Add(new { Name = str, Value = Convert.ToInt32(Enum.Parse(typeof(MilkType), str)) });
            }
            cmbMilkType.DataSource = _MilkTypes;

            ResetForm();
        }
        #endregion

        #region Events
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ValidateForm();
                Model.Name = txtName.Text;
                Model.Address = txtAddress.Text;
                Model.BankNo = txtBankNo.Text;
                Model.Code = txtCode.Text;
                Model.Gender = rdoMale.Checked;
                Model.ContactNo = Convert.ToInt32((string.IsNullOrEmpty(txtContactNo.Text)) ? "0" : txtContactNo.Text);
                Model.IsMember = chkIsMember.Checked;
                Model.MilkType = (MilkType)cmbMilkType.SelectedValue;

                BL.Transaction(true);
                ResetForm();
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void ValidateForm()
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                txtName.Focus();
                throw new Exception("Name should not be blank.");
            }
            else if (string.IsNullOrEmpty(txtCode.Text))
            {
                txtCode.Focus();
                throw new Exception("Code should not be blank.");
            }

            _blSabhasad.Search("Code = " + txtCode.Text);

            if (_DSSearch.Tables.Contains("Sabhasad") && _DSSearch.Tables["Sabhasad"].Rows.Count > 0)
            {
                if (Model.TransactionMode == TransactionModes.Update)
                {
                    if (_DSSearch.Tables["Sabhasad"].Rows.Count > 1)
                    {
                        txtCode.Focus();
                        throw new Exception("The Code cannot be repeated.");
                    }
                }
                else
                {
                    txtCode.Focus();
                    throw new Exception("The Code cannot be repeated.");
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            frmSearch_Eng _frmSearch = new frmSearch_Eng();
            _frmSearch.Hidden_Field.Add("SabhasadId");
            _frmSearch.Hidden_Field.Add("IsActive");
            _frmSearch.BusinessClass = BL;
            _frmSearch.ShowDialog();
            if (_frmSearch.PressedButton == Press_Button.Edit)
            {
                DataRow _dr = _DS.Tables["Sabhasad"].Rows[0];
                Model.SabhasadId = Convert.ToInt32(_dr["SabhasadId"]);
                txtAddress.Text = _dr["Address"].ToString();
                txtName.Text = _dr["Name"].ToString();
                txtCode.Text = _dr["Code"].ToString();
                txtBankNo.Text = _dr["BankNo"].ToString();
                cmbMilkType.SelectedValue = _dr["MilkType"];
                chkIsMember.Checked = Convert.ToBoolean(_dr["IsMember"]);
                txtContactNo.Text = _dr["ContactNo"].ToString();
            }
            else
            {
                ResetForm();
            }
        }
        #endregion

        #region Private Methods
        void ResetForm()
        {
            foreach (Control ctl in this.Controls.GetControls<ArcTextBox>())
                ctl.Text = string.Empty;

            Model.SetDefaultValues();
            string _NextCode = BL.GetNextSabhasadCode();

            txtCode.Text = _NextCode;

            txtName.Focus();
        }
        #endregion

        private void btnNew_Click(object sender, EventArgs e)
        {
            ResetForm();
        }

        private void txtContactNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }
    }
}
